package Form;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.DefaultListModel;

import model.entities.MaintenanceStation;
import model.entities.WeatherStation;
import DBConnection.MaintenanceStationDB;
import DBConnection.WeatherStationDB;

public class MaintenanceStationHandling {

	private List<WeatherStationForm> lWeatherSF = null;
	private Timer timer = null;
	private MaintenanceStation maintenanceS = null;
	private String maintenanceSID = null;

	public MaintenanceStationHandling(String maintenanceSID) {
		this.maintenanceSID = maintenanceSID;
		maintenanceS = new MaintenanceStationDB().find(maintenanceSID);
		this.lWeatherSF = new ArrayList<WeatherStationForm>();
		/* updateListStation(); */
		List<WeatherStation> lWS = new WeatherStationDB().findAll();
		for (WeatherStation weatherS : lWS) {
			WeatherStationForm weatherSF = new WeatherStationForm(
					weatherS.getWSID());
			weatherSF.endTaskUpdate();
			weatherSF.setTitle(weatherS.getWSID() + " is controlling...");
			weatherSF
					.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
			weatherSF.showComponentAdmin();
			lWeatherSF.add(weatherSF);
		}
		taskRefreshStation();

	}

	/*
	 * Ham nay dung de cap nhat lai reportKhi ma ben may tram WeatherStation gui
	 * reportThi ben may MaintenanceStation se cap nhat laiDong thoi se phan hoi
	 * reportreponse neu can thietCu 5s se cap nhat 1 lan
	 */
	private void taskRefreshStation() {
		timer = new Timer();
		TimerTask tt = new TimerTask() {

			@Override
			public void run() {

				for (WeatherStationForm weatherSF : lWeatherSF) {
					WeatherStation weatherStation = new WeatherStationDB()
							.find(weatherSF.getWeatherSH().getStationID());
					weatherSF.getWeatherSH().setWeatherStation(weatherStation);

					weatherSF.getWeatherSH().setlReport(
							weatherSF.getWeatherSH().getAllReport());
					weatherSF.showOnForm();

				}

			}
		};
		timer.schedule(tt, 0, 5000);
	}

	/*
	 * Ham nay dung de kiem tra so tram bi hu hongTra ve so may bi hong
	 */
	public int numberStationDamaged() {
		int numberStationDamaged = 0;
		for (WeatherStationForm weatherSF : lWeatherSF) {
			if (weatherSF.getWeatherSH().getWeatherStation().getDamaged() == 1)
				numberStationDamaged++;
		}
		return numberStationDamaged;
	}

	public DefaultListModel<String> getDefaultListModelWSF() {
		DefaultListModel<String> dlmWeatherSF = new DefaultListModel<String>();
		for (int i = 0; i < lWeatherSF.size(); i++) {
			dlmWeatherSF.addElement("WeatherStation "
					+ lWeatherSF.get(i).getWeatherSH().getStationID());
		}
		return dlmWeatherSF;
	}

	/*
	 * Ham nay dung de hien thi tram sau khi nhan nut View Dong thoi chiem quyen
	 * cua tram da duoc chon de View
	 */
	public void showWeatherStationForm(int selectedIndex) {
		getlWeatherSF().get(selectedIndex).getWeatherSH().setAdminRole(true);
		getlWeatherSF().get(selectedIndex).setVisible(true);
	}

	/*
	 * Ham nay dung de nha quyen Admin toi cac may WeatherStation
	 */
	public void releaseAdminRole() {
		for (WeatherStationForm weatherSF : lWeatherSF) {
			weatherSF.getWeatherSH().getWeatherStation().setAdminRole(0);
			weatherSF.getWeatherSH().updateDatabaseWeatherStation();
		}

	}

	/*
	 * Ham nay dung de kiem tra controlAll la true hay false Neu la true thi
	 * thiet lap quyen admin la true Dong thoi thiet lap dong cua so la
	 * HIDE_ON_Close Nguoc lai set quyen admin la false Dong thoi thiet lap dong
	 * cua so la DISPOSE_ON_CLOSE
	 */
	public boolean controlAll(boolean status) {
		try {
			if (status) {
				for (WeatherStationForm weatherSF : lWeatherSF) {
					weatherSF
							.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
					weatherSF.getWeatherSH().setAdminRole(true);
					/* weatherSF.setVisible(true); */
				}
			} else {
				for (WeatherStationForm weatherSF : lWeatherSF) {
					weatherSF
							.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
					weatherSF.getWeatherSH().setAdminRole(false);
				}
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/*
	 * Ham nay dung de Search tram WeatherStation Nhan vao 1 chuoi va tra ve
	 * list cac tram co ten gan giong
	 */
	public List<String> Search(String name) {
		List<String> lResult = new ArrayList<String>();
		for (WeatherStationForm weatherSF : lWeatherSF) {
			if (weatherSF.getWeatherSH().getStationID().indexOf(name) >= 0) {
				lResult.add(weatherSF.getWeatherSH().getStationID());
			}
		}
		return lResult;
	}

	/*
	 * Ham nay dung de them 1 tram Station moi vao database
	 */
	public boolean addNewStation(String idStation) {
		WeatherStation weatherStation = new WeatherStation(idStation,
				getMaintenanceSID(), "SS01", 100, 0, 0, 1, 0, 0);
		if (checkAvailableId(idStation)) {
			new WeatherStationDB().add(weatherStation);
			WeatherStationForm wSF = new WeatherStationForm(idStation);
			wSF.endTaskUpdate();
			wSF.setTitle(weatherStation.getWSID() + " is controlling...");
			wSF.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
			wSF.showComponentAdmin();
			lWeatherSF.add(wSF);
			return true;
		} else
			return false;

	}

	/* Ham nay dung de kiem tra Id co ton tai hay chua */
	private boolean checkAvailableId(String idStation) {
		// Lay het tat ca cac tram Weather Station len
		List<WeatherStation> lWeather = new WeatherStationDB().findAll();
		// Sau do kiem tra
		for (WeatherStation wS : lWeather) {
			{
				// Neu ma co su trung ID thi return false
				if (wS.getWSID().equals(idStation))
					return false;
			}
		}
		// Khong thi return true
		return true;
	}

	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}

	public List<WeatherStationForm> getlWeatherSF() {
		return lWeatherSF;
	}

	public void setlWeatherSF(List<WeatherStationForm> lWeatherSF) {
		this.lWeatherSF = lWeatherSF;
	}

	public MaintenanceStation getMaintenanceS() {
		return maintenanceS;
	}

	public void setMaintenanceS(MaintenanceStation maintenanceS) {
		this.maintenanceS = maintenanceS;
	}

	public String getMaintenanceSID() {
		return maintenanceSID;
	}

	public void setMaintenanceSID(String maintenanceSID) {
		this.maintenanceSID = maintenanceSID;
	}
}
